<template>
  <view class="product-detail-container">
    <!-- 返回按钮 -->
    <view class="return-btn" @click="goToHome" hover-class="none">
      <image src="/static/logo/返回.png" mode="widthFix" class="return-icon"></image>
    </view>

    <!-- 商品主图 -->
    <view class="product-main-image">
      <image :src="product.image" class="product-image" mode="widthFix"></image>
    </view>

    <!-- 商品基本信息 -->
    <view class="product-info">
      <text class="product-name">{{ product.name }}</text>
      <text class="product-desc">{{ product.description || '青城山道教文化创意产品，精选材质，匠心制作' }}</text>

      <view class="price-section">
        <text class="current-price">{{ product.price }}</text>
        <text class="original-price">￥{{ product.originalPrice }}</text>
        <text class="discount">{{ product.discount }}</text>
      </view>

      <view class="meta-info">
        <view class="meta-item">
          <text class="meta-label">销量</text>
          <text class="meta-value">{{ product.sales }}件</text>
        </view>
        <view class="meta-item">
          <text class="meta-label">评价</text>
          <text class="meta-value">{{ product.commentCount }}条</text>
        </view>
        <view class="meta-item">
          <text class="meta-label">评分</text>
          <text class="meta-value">{{ product.rating }}分</text>
        </view>
      </view>
    </view>

    <!-- 商品详情 -->
    <view class="detail-section">
      <view class="section-header">
        <text class="section-title">商品详情</text>
      </view>
      <view class="detail-content">
        <view class="detail-item" v-for="(item, index) in product.detailItems" :key="index">
          <text class="detail-title">{{ item.title }}</text>
          <text class="detail-text">{{ item.content }}</text>
          <image v-if="item.image" :src="item.image" mode="widthFix" class="detail-image"></image>
        </view>
      </view>
    </view>

    <!-- 底部操作栏 -->
    <view class="action-bar">
      <button class="add-to-cart" @click="addToCart">加入购物车</button>
      <button class="buy-now" @click="buyNow">立即购买</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      productId: null,
      product: {
        image: "",
        name: "",
        price: "",
        originalPrice: "",
        discount: "",
        sales: 0,
        commentCount: 0,
        rating: 0,
        detailItems: []
      }
    };
  },
  onLoad(options) {
    this.productId = options.id;
    this.loadProductDetail();
  },
  methods: {
    goToHome() {
      uni.switchTab({
        url: '/pages/shop/shop'
      });
    },

    loadProductDetail() {
      setTimeout(() => {
        const product = this.findProductById(this.productId);
        if (product) {
          this.product = {
            ...product,
            originalPrice: product.originalPrice || (parseFloat(product.price.replace('￥', '')) * 1.2).toFixed(1),
            discount: product.discount || '8折',
            commentCount: product.commentCount || Math.floor(product.sales * 0.8),
            detailItems: this.parseDetailItems(product.detail)
          };
        } else {
          uni.showToast({
            title: '商品不存在',
            icon: 'none'
          });
          setTimeout(() => {
            uni.navigateBack();
          }, 1500);
        }
      }, 300);
    },

    parseDetailItems(detail) {
      // 这里可以根据实际数据结构进行调整
      return [
        { title: '产品介绍', content: this.getProductDescription(), image: this.product.image },
        { title: '产品参数', content: this.getProductParams() },
        { title: '温馨提示', content: this.getProductTips() }
      ];
    },

    getProductDescription() {
      return this.product.description || '青城山道教文化创意产品，精选材质，匠心制作';
    },

    getProductParams() {
      // 根据商品ID返回不同的参数
      const params = {
        1: '材质：优质竹材+宣纸\n尺寸：展开宽度30cm，高度15cm\n工艺：手工绘制',
        2: '材质：优质棉麻\n尺寸：35cm×40cm\n承重：最大5kg',
        3: '材质：优质黄铜\n尺寸：15cm×3cm\n工艺：激光雕刻',
        4: '材质：优质棉质\n尺寸：均码，可调节\n工艺：刺绣工艺',
        5: '材质：优质TPU软胶\n适用机型：iPhone/华为/小米等主流机型'
      };
      return params[this.productId] || '优质材质，匠心工艺';
    },

    getProductTips() {
      return '1. 请勿长时间暴晒或浸泡\n2. 轻拿轻放，避免剧烈碰撞\n3. 建议手洗，阴凉处晾干';
    },

    findProductById(id) {
      const products = [
        {
          id: 1,
          name: "青城山文创折扇",
          price: "￥32",
          sales: 300,
          rating: 4.5,
          image: "https://lbwnb123.oss-cn-beijing.aliyuncs.com/%E9%9D%92%E5%9F%8E/%E5%95%86%E5%9F%8E/%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8/%E9%9D%92%E5%9F%8E%E5%B1%B1%E6%96%87%E5%88%9B%E6%8A%98%E6%89%87.png",
          description: "手工竹制折扇，绘有青城山道教文化图案"
        },
        {
          id: 2,
          name: "青城山文创帆布袋",
          price: "￥15",
          sales: 500,
          rating: 4.8,
          image: "http://lbwnb123.oss-cn-beijing.aliyuncs.com/%E9%9D%92%E5%9F%8E/%E5%95%86%E5%9F%8E/%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8/%E5%B8%86%E5%B8%83%E8%A2%8B%20%281%29.png",
          description: "环保棉麻帆布袋，印有青城山特色图案"
        },
        {
          id: 3,
          name: "青城山文创书签",
          price: "￥10",
          sales: 900,
          rating: 4.9,
          image: "https://lbwnb123.oss-cn-beijing.aliyuncs.com/%E9%9D%92%E5%9F%8E/%E5%95%86%E5%9F%8E/%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8/%E4%B9%A6%E7%AD%BE.png",
          description: "黄铜材质书签，雕刻精美道教图案"
        },
        {
          id: 4,
          name: "青城山文创鸭舌帽",
          price: "￥28",
          sales: 400,
          rating: 4.0,
          image: "https://lbwnb123.oss-cn-beijing.aliyuncs.com/%E9%9D%92%E5%9F%8E/%E5%95%86%E5%9F%8E/%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8/%E9%B8%AD%E8%88%8C%E5%B8%BD.png",
          description: "时尚鸭舌帽，绣有青城山标志"
        },
        {
          id: 5,
          name: "青城山文创手机壳",
          price: "￥12",
          sales: 400,
          rating: 4.0,
          image: "http://lbwnb123.oss-cn-beijing.aliyuncs.com/%E9%9D%92%E5%9F%8E/%E5%95%86%E5%9F%8E/%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8/%E6%89%8B%E6%9C%BA%E5%A3%B3%20%282%29%20%281%29.png",
          description: "适用iPhone/华为等机型，道教文化设计"
        },
        {
          id: 6,
          name: "青城山文创方形抱枕",
          price: "￥19.9",
          sales: 530,
          rating: 4.2,
          image: "http://lbwnb123.oss-cn-beijing.aliyuncs.com/%E9%9D%92%E5%9F%8E/%E5%95%86%E5%9F%8E/%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8/%E6%96%B9%E5%BD%A2%E6%8A%B1%E6%9E%95%EF%BC%881%EF%BC%89.png",
          description: "方形抱枕，印有青城山风景图案"
        },
        {
          id: 7,
          name: "青城山文创明信片",
          price: "￥9.9",
          sales: 1030,
          rating: 4.7,
          image: "http://lbwnb123.oss-cn-beijing.aliyuncs.com/%E9%9D%92%E5%9F%8E/%E5%95%86%E5%9F%8E/%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8/%E6%98%8E%E4%BF%A1%E7%89%87%20%281%29.png",
          description: "一套8张，包含青城山著名景点"
        },
        {
          id: 8,
          name: "青城山文创杯垫",
          price: "￥9.9",
          sales: 830,
          rating: 4.3,
          image: "http://lbwnb123.oss-cn-beijing.aliyuncs.com/%E9%9D%92%E5%9F%8E/%E5%95%86%E5%9F%8E/%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8/%E6%9D%AF%E5%9E%AB%20%281%29.png",
          description: "陶瓷杯垫，印有道教文化图案"
        },
        {
          id: 9,
          name: "青城山文创T恤",
          price: "￥39.9",
          sales: 2830,
          rating: 4.6,
          image: "http://lbwnb123.oss-cn-beijing.aliyuncs.com/%E9%9D%92%E5%9F%8E/%E5%95%86%E5%9F%8E/%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8/T%E6%81%A4%20%281%29.png"
          ,
          description: "纯棉T恤，胸前印有青城山标志"
        },
        {
          id: 10,
          name: "青城山文创钥匙扣",
          price: "￥5.9",
          sales: 1830,
          rating: 4.4,
          image: "http://lbwnb123.oss-cn-beijing.aliyuncs.com/%E9%9D%92%E5%9F%8E/%E5%95%86%E5%9F%8E/%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8/%E9%92%A5%E5%8C%99%E6%89%A3%20%281%29.png",
          description: "金属钥匙扣，雕刻道教文化符号"
        }
      ];      return products.find(item => item.id == id);
    },

    addToCart() {
      uni.showToast({
        title: '已加入购物车',
        icon: 'success'
      });
    },

    buyNow() {
      uni.navigateTo({
        url: `/pages/order/create?productId=${this.productId}&quantity=1`
      });
    }
  }
};
</script>

<style lang="scss">
.product-detail-container {
  padding-bottom: 100px;
  background-color: #f8f8f8;
}

.return-btn {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 10;
  width: 40px;
  height: 40px;

  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;

  .return-icon {
    width: 24px;
    height: 24px;
  }
}

.product-main-image {
  width: 100%;
  height: 375px;
  background-color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;

  .product-image {
    width: 100%;
    height: auto;
    max-height: 100%;
  }
}

.product-info {
  padding: 20px;
  background-color: #fff;
  margin-bottom: 10px;
  border-radius: 0 0 10px 10px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);

  .product-name {
    font-size: 20px;
    font-weight: bold;
    color: #333;
    display: block;
    margin-bottom: 8px;
  }

  .product-desc {
    color: #666;
    font-size: 14px;
    line-height: 1.5;
    display: block;
    margin-bottom: 15px;
  }

  .price-section {
    display: flex;
    align-items: center;
    margin-bottom: 15px;

    .current-price {
      color: #e74c3c;
      font-size: 24px;
      font-weight: bold;
      margin-right: 10px;
    }

    .original-price {
      color: #999;
      font-size: 14px;
      text-decoration: line-through;
      margin-right: 10px;
    }

    .discount {
      background-color: #e74c3c;
      color: #fff;
      font-size: 12px;
      padding: 2px 5px;
      border-radius: 4px;
    }
  }

  .meta-info {
    display: flex;
    justify-content: space-between;
    padding-top: 10px;
    border-top: 1px dashed #eee;

    .meta-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      flex: 1;

      .meta-label {
        color: #999;
        font-size: 12px;
        margin-bottom: 3px;
      }

      .meta-value {
        color: #333;
        font-size: 14px;
        font-weight: 500;
      }
    }
  }
}

.detail-section {
  background-color: #fff;
  margin-bottom: 10px;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);

  .section-header {
    padding: 15px;
    border-bottom: 1px solid #f5f5f5;

    .section-title {
      font-size: 16px;
      font-weight: bold;
      color: #333;
      position: relative;
      padding-left: 10px;

      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 3px;
        height: 16px;
        background-color: #e74c3c;
        border-radius: 2px;
      }
    }
  }

  .detail-content {
    padding: 15px;

    .detail-item {
      margin-bottom: 20px;

      &:last-child {
        margin-bottom: 0;
      }

      .detail-title {
        display: block;
        font-size: 15px;
        font-weight: bold;
        color: #333;
        margin-bottom: 8px;
        position: relative;
        padding-left: 15px;

        &::before {
          content: '';
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
          width: 8px;
          height: 8px;
          background-color: #e74c3c;
          border-radius: 50%;
        }
      }

      .detail-text {
        display: block;
        font-size: 14px;
        color: #666;
        line-height: 1.8;
        white-space: pre-line;
        margin-bottom: 10px;
      }

      .detail-image {
        width: 100%;
        height: auto;
        border-radius: 5px;
        margin-top: 10px;
      }
    }
  }
}

.action-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 60px;
  display: flex;
  background-color: #fff;
  border-top: 1px solid #eee;
  padding: 5px 10px;
  box-sizing: border-box;

  button {
    flex: 1;
    height: 50px;
    line-height: 50px;
    text-align: center;
    font-size: 16px;
    border-radius: 25px;
    margin: 0 5px;
    transition: all 0.3s;

    &.add-to-cart {
      background-color: #ff9500;
      color: #fff;

      &:active {
        background-color: darken(#ff9500, 10%);
      }
    }

    &.buy-now {
      background-color: #e74c3c;
      color: #fff;

      &:active {
        background-color: darken(#e74c3c, 10%);
      }
    }
  }
}
</style>
